package com.ruoyi.hr.service.impl.welfare.welfarechange;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.welfare.welfarechange.WelfareChange;
import com.ruoyi.hr.mapper.SkyMsgAlarmMapper;
import com.ruoyi.hr.mapper.welfare.welfareInfo.WelfareInfoMapper;
import com.ruoyi.hr.mapper.welfare.welfarechange.WelfareChangeMapper;
import com.ruoyi.hr.service.welfare.welfarechange.WelfareChangeService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.hr.util.ResultEnum;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class WelfareChangeServiceImpl implements WelfareChangeService {

    @Autowired
    WelfareChangeMapper welfareChangeMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    WelfareInfoMapper welfareInfoMapper;

    @Autowired
    SkyMsgAlarmMapper skyMsgAlarmMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<WelfareChange> findDataWelfareChange(WelfareChange welfareChange, List<Authority> authorities, int isAdmin) {
        return welfareChangeMapper.findDataWelfareChange(welfareChange, authorities, isAdmin);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public String executeProcedure(String sql) {
        return welfareChangeMapper.executeProcedure(sql);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult addChangeProcedure(String allPeid) {
        //叠加异常信息
        String msgAdd = "";
        try {
            //存储过程sql
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allPeid.split(",");
            String msg;//异常信息
            for (String s : array) {
                //根据PEID取出人名
                Map<String, Object> map = welfareInfoMapper.findNameByPeid(s);
                sql = "declare @RetVal varchar(50) exec cSP_BeneChangeAdd " + s + ",100," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = welfareChangeMapper.executeProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += map.get("name") + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public BaseResult changeProcedure(String allId, String procedure) {
        //叠加异常信息
        String msgAdd = "";
        try {
            String sql;
            SysUser user = ShiroUtils.getSysUser();
            Map<String, Object> maps = sysUserMapper.selectSkySecUser(user.getLoginName());
            String[] array = allId.split(",");
            String msg;//异常信息
            for (String id : array) {
                //根据id取出人名
                WelfareChange welfareChange = welfareChangeMapper.findWelfareChangeById(id);
                sql = "declare @RetVal varchar(50) exec " + procedure + " " + id + ",100," + maps.get("id").toString() + ",@RetVal output select @RetVal";
                String retVal = welfareChangeMapper.executeProcedure(sql);
                msg = skyMsgAlarmMapper.getMsgById(retVal);
                if (msg != null) {
                    msgAdd += welfareChange.getName() + ":" + msg + "\n";
                }
            }
        } catch (Exception e) {
            return BaseResult.build(ResultEnum.SERVER_ERROR);
        }
        if ("".equals(msgAdd)) {
            return BaseResult.build(ResultEnum.SUCCESS);
        } else {
            return BaseResult.build(19970926, msgAdd, null);
        }
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public WelfareChange findWelfareChangeById(String id) {
        return welfareChangeMapper.findWelfareChangeById(id);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateWelfareChange(WelfareChange welfareChange) {
        return welfareChangeMapper.updateWelfareChange(welfareChange);
    }
}
